package com.example.myapplication.ui.income;


import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.entity.Finance;
import com.example.myapplication.entity.Income;
import com.example.myapplication.greendao.DaoSession;
import com.example.myapplication.greendao.FinanceDao;
import com.example.myapplication.greendao.IncomeDao;
import com.example.myapplication.ui.borrow.BorrowFragment;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;


public class IncomeFragment extends Fragment {

    private Button b1, b2, b4;
    private App app;
    private DaoSession daoSession;
    private Spinner spinner;
    private String element;
    private int s_position;
    private String payType;
    private int startYear, startMonth, startDay;
    private Calendar calendar;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_income, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        calendar = Calendar.getInstance();
        startYear = calendar.get(Calendar.YEAR);
        startMonth = calendar.get(Calendar.MONTH);
        startDay = calendar.get(Calendar.DAY_OF_MONTH);
        b1 = getActivity().findViewById(R.id.button_1);
        b4 = getActivity().findViewById(R.id.button_4);
        b2 = getActivity().findViewById(R.id.incomeTime);
        spinner = getActivity().findViewById(R.id.spinner2);
        spinner.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
        setListeners();
    }

    private class Onclick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_1:
                    app = (App) getActivity().getApplication();
                    daoSession = app.getDaoSession();
                    EditText t1 = getActivity().findViewById(R.id.text_1);
                    EditText t2 = getActivity().findViewById(R.id.text_2);
                    IncomeDao incomeDao = daoSession.getIncomeDao();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                    Date date = null;
                    try {
                        date = df.parse(String.valueOf(b2.getText()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    double incomeVal = Double.parseDouble(t1.getText().toString());
                    String remark = t2.getText().toString();
                    System.out.println(incomeVal);
                    System.out.println(remark);
                    Income income = new Income();
                    income.setIncome(incomeVal);
                    income.setIncomeDate(date);
                    income.setIncomeRemarks(remark);
                    income.setIncomeType(payType);
                    incomeDao.insert(income);
                    Toast.makeText(getContext(), "插入成功", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button_4:
                    app = (App) getActivity().getApplication();
                    daoSession = app.getDaoSession();
                    IncomeDao incomeDao1 = daoSession.getIncomeDao();
                    List<Income> incomeList = incomeDao1.loadAll();
                    ListView listView = getActivity().findViewById(R.id.list);
                    List<String> list = new ArrayList<String>();
                    for (Income income1 : incomeList) {
                        list.add("收入：" + income1.getIncome() + " " + "收入说明：" + income1.getIncomeRemarks() + " "
                                + "时间：" + income1.getIncomeDate() + "" + "收入类型" +income1.getIncomeType());
                    }
                    ///可以一直添加，在真机运行后可以下拉列表
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, list);
                    listView.setAdapter(adapter);
                    break;
                case R.id.incomeTime:
                    new DatePickerDialog(getContext(),
                            startSetListener, startYear, startMonth, startDay).show();
                    break;
            }
        }
    }

    private DatePickerDialog.OnDateSetListener startSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            startYear = year;
            startMonth = monthOfYear;
            startDay = dayOfMonth;
            //更新Button上显示的日期
            updateDateStartDisplay();
        }
    };

    private void updateDateStartDisplay() {
        b2.setText(new StringBuilder().append(startYear).append("-")
                .append(startMonth + 1).append("-").append(startDay));
    }


    private void setListeners() {
        Onclick onclick = new Onclick();
        b1.setOnClickListener(onclick);
        b2.setOnClickListener(onclick);
        b4.setOnClickListener(onclick);
    }

    // 下拉框选择事件
    private class OnItemSelectedListenerImpl implements AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            element = parent.getItemAtPosition(position).toString();// 得到spanner的值
            s_position = position;
            switch (s_position) {
                case 0:
                    payType = "income";
                    break;
                case 1:
                    payType = "reword";
                    break;
                case 2:
                    payType = "other";
                    break;

            }
            Toast.makeText(getActivity(), "选择的元素是：" +
                    element, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    }


}